package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.ProductFeignService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {
@Autowired
private ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * key: 0
         * status: 0
         * wareId: 1
         */
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        String key= (String) params.get("key");
        String status=(String)params.get("status");
        String wareId=(String)params.get("wareId");
        if (StringUtils.isNotEmpty(key)){
            wrapper.and((w)->{
                w.eq("id",key).or().eq("sku_id",key).or().like("sku_num",key).or().like("sku_price",key);
            });
        }
        if (StringUtils.isNotEmpty(status)){
            wrapper.eq("status",status);
        }
        if (StringUtils.isNotEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page( new Query<PurchaseDetailEntity>().getPage(params),wrapper);

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> detailEntities=this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id",id));
        return detailEntities;
    }

    @Override
    public void saveWithSkuPrice(PurchaseDetailEntity purchaseDetail) {
        PurchaseDetailEntity entity = new PurchaseDetailEntity();
        //lack sku_price =sku_num*price Integer sku_num=purchaseDetail.getSkuNum();
        BeanUtils.copyProperties(purchaseDetail,entity);
        Integer skuNum=purchaseDetail.getSkuNum();
        Long skuId=purchaseDetail.getSkuId();
        R info = productFeignService.info(skuId);
        Map<String,Object>data= (Map<String, Object>) info.get("skuInfo");
        if (info.getCode()==0){
            entity.setSkuPrice(new BigDecimal(data.get("price").toString()).multiply(BigDecimal.valueOf(skuNum)));
        }
        this.save(entity);
    }

}